package com.ply.flashsalessystem.schedule;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ply.flashsalessystem.entity.OrderRecord;
import com.ply.flashsalessystem.entity.pojo.*;
import com.ply.flashsalessystem.entity.status.OrderStatus;
import com.ply.flashsalessystem.entity.status.UserAmountStatus;
import com.ply.flashsalessystem.mapper.OrderTimeMapper;
import com.ply.flashsalessystem.service.*;
import com.ply.flashsalessystem.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author XiaoPan
 * date: 2022/1/19 10:36
 * <p>
 * action:
 */
@Component
@Slf4j
public class MySchedule {
    @Autowired
    OrderForGoodsService orderForGoodsService;

    @Autowired
    OrderTimeService orderTimeService;
//
    @Autowired
    OrderTimeMapper orderTimeMapper;

    @Autowired
    OrderRecordService orderRecordService;

    @Autowired
    UserService userService;

    @Autowired
    UserAmountService userAmountService;

    @Autowired
    StoreService storeService;

    @Autowired
    ReturnOfGoodsService returnOfGoodsService;

    @Autowired
    ReturnOfGoodsDetailsService returnOfGoodsDetailsService;

    @Scheduled(cron = "0 0/1 * * * ?")
    public void sch(){
        updateOrderAutoChanel();
        orderToOver();
    }

    // 定时 取消 订单
    @Transactional
    public void updateOrderAutoChanel(){
        log.debug("定时器 定时 取消 订单");
        OrderTime byId = orderTimeService.query(1);
        int orderChanelTime = byId.getOrderChanelTime();

        QueryWrapper<OrderForGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", OrderStatus.NO_PAY);
        List<OrderForGoods> list = orderForGoodsService.list(queryWrapper);
        for (OrderForGoods orderForGoods : list) {
            Date createTime = orderForGoods.getCreateTime();
            //创建时间
            LocalDateTime localDateTime = DateUtils.DateTo(createTime);
            localDateTime = localDateTime.plusMinutes(orderChanelTime);
            LocalDateTime localDateTime1 = LocalDateTime.now();
            //比较时间 执行 修改 状态
            if (localDateTime1.compareTo(localDateTime) > 0){
                orderForGoodsService.cancelOrder(orderForGoods.getOfgId());
                log.debug("过期时间:{}, 执行成功",localDateTime);
            }
            log.debug("过期时间:{}",localDateTime);
        }
        log.info("执行定时器");
    }

    //定时 订单结束  有退货的话 需要判断 退货的 状态 退货的金额
    @Transactional
    public void orderToOver(){
        log.debug("定时器 订单结束 开始");
        // 订单结束, 退钱到 用户账户   抽成 到 店家账户 修改订单状态
        OrderTime byId = orderTimeService.query(1);
        //提成 1-100
        int commission = byId.getCommission();
        //天数
        int orderOverEnd = byId.getOrderOverEnd();

        QueryWrapper<OrderForGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", OrderStatus.USER_DO_SIGN);
        List<OrderForGoods> list = orderForGoodsService.list(queryWrapper);
        for (OrderForGoods orderForGoods : list) {
            //获取 订单的 状态 转变
            QueryWrapper<OrderRecord> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("ofg_id",orderForGoods.getOfgId());
            queryWrapper1.eq("status",OrderStatus.USER_DO_SIGN);
//            OrderRecord one = orderRecordService.getOne(queryWrapper1);

            List<OrderRecord> list1 = orderRecordService.list(queryWrapper1);

//            if (one == null){
//                continue;
//            }
            if (list1.isEmpty()){
                continue;
            }
            OrderRecord one = list1.get(0);

            //签收时间
            Date createTime = one.getCreateTime();

            //创建时间
            LocalDateTime localDateTime = DateUtils.DateTo(createTime);
            localDateTime = localDateTime.plusDays(orderOverEnd);
            LocalDateTime localDateTime1 = LocalDateTime.now();

            //比较时间 执行 修改 状态
            if (localDateTime1.compareTo(localDateTime) > 0){
                //获取用户id
                Long userId = orderForGoods.getUserId();

                // 可以 修改
                User byId1 = userService.getById(userId);
                // 退钱的
                byId1.setBalanceMoney(byId1.getBalanceMoney() + orderForGoods.getAmountReturn());

                //修改用户 余额
                userService.updateById(byId1);

                // 增加 用户 账户记录
                UserAmount userAmount = new UserAmount();
                userAmount.setAmount(orderForGoods.getAmountReturn());
                userAmount.setUserId(userId);
                userAmount.setStatus(UserAmountStatus.Return);
                userAmountService.save(userAmount);

//                商家 抽成
                //获取 商家id
                Long storeId = orderForGoods.getStoreId();
//                抽成 金额
                double v = orderForGoods.getAmountPaid() / 100 * commission;

                // 抽成方法 里面 有 店家余额 增加    店家 账户记录 修改
                storeService.commission(storeId,v);

                //修改 订单 状态
                orderForGoodsService.statusToEnd(orderForGoods.getOfgId());
                log.debug("过期时间:{}, 执行成功",localDateTime);
            }
            log.debug("过期时间:{}",localDateTime);
        }
        log.info("执行定时器");

    }
}
